package com.scoe.gcs.comms.activites;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;

import android.app.Activity;
import android.content.Intent;
import com.MAVLink.MAVLink;
import com.MAVLink.Messages.IMAVLinkMessage;
import com.MAVLink.Messages.common.msg_gps_status;
import com.MAVLink.Messages.common.msg_request_data_stream;
import com.MAVLink.Messages.common.msg_statustext;
import com.MAVLink.Messages.common.msg_sys_status;
import com.scoe.gcs.comms.CommonSettings;
import com.scoe.gcs.comms.CommsProxy;
import com.scoe.gcs.comms.AC1Data.ProtocolParser;
import com.scoe.gcs.comms.CommunicationClient;

public class StatusActivity{

	private Activity parent;
	
	private KrollProxy proxy = new KrollProxy();
	private KrollDict props = new KrollDict();
	
	private CommsProxy commsProxy = new CommsProxy();
	
	private TiApplication appContext = TiApplication.getInstance();
	private Activity rootActivity = appContext.getCurrentActivity();
	
	public StatusActivity(Activity p){
		parent = p;
		ba.init();
	}
	
	public void destroyComms(){
		ba.onDestroy();
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		ba.onActivityResult(requestCode, resultCode, data);
		
	}

	CommunicationClient ba = new CommunicationClient(rootActivity) {
		
		@Override
		public void notifyConnected() {
			if( CommonSettings.isProtocolAC1())
				sendBytesToComm(ProtocolParser.requestFlightData());
			else if (CommonSettings.isProtocolMAVLink()){
				
				msg_request_data_stream req = new msg_request_data_stream();
				req.req_message_rate = 20;
				req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_EXTRA1; // MAV_DATA_STREAM_RAW_CONTROLLER;
																					// MAV_DATA_STREAM_RAW_SENSORS;
				req.start_stop = 1;
				req.target_system = MAVLink.CURRENT_SYSID;
				req.target_component = 0;
				//ba.sendBytesToComm( MAVLink.createMessage(req));
				
				/* moved to gps activity
				req = new msg_request_data_stream();
				req.req_message_rate = 1;
				req.req_stream_id = MAVLink.MAV_DATA_STREAM.MAV_DATA_STREAM_EXTENDED_STATUS; // MAV_DATA_STREAM_RAW_CONTROLLER;
																							 // MAV_DATA_STREAM_RAW_SENSORS;
				req.start_stop = 1;
				req.target_system = MAVLink.CURRENT_SYSID;
				req.target_component = 0;
				ba.sendBytesToComm( MAVLink.createMessage(req));
				*/
			}
		}

		@Override
		public void notifyDisconnected() {

		}

		@Override
		public void notifyDeviceNotAvailable() {

		}

		@Override
		public void notifyReceivedData(int count, IMAVLinkMessage m) {
			if( CommonSettings.isProtocolAC1()){
//				if( FlightData.class.isInstance(m)){
//					FlightData fd = (FlightData)m;
//					double g[] = {fd.gyroX, fd.gyroY, fd.gyroYaw};
//					double a[] = {fd.accelX, fd.accelY, fd.accelZ};
//					double mag[] = {fd.magX, fd.magY, fd.magZ};
//
//					float roll = (float)-Math.atan2(a[0], a[2]), 
//						 pitch = (float)-Math.atan2(a[1], a[2]), 
//						 yaw=0;
//										
//					notifyTitaniumHUD(roll, pitch, yaw);
//					
//				}
				
			}else if (CommonSettings.isProtocolMAVLink()){
				switch(m.messageType){
					case msg_sys_status.MAVLINK_MSG_ID_SYS_STATUS:{
						msg_sys_status msg = (msg_sys_status) m;
						commsProxy.notifyTitaniumBatStatus(msg.battery_remaining/10.0, msg.voltage_battery);
						break;
					}	

					case msg_gps_status.MAVLINK_MSG_ID_GPS_STATUS:{
						msg_gps_status msg = (msg_gps_status) m;
						commsProxy.notifyTitaniumGPSStatus(msg.satellites_visible);
						break;
					}
					
					case msg_statustext.MAVLINK_MSG_ID_STATUSTEXT:{
						msg_statustext msg = (msg_statustext) m;
						int severity = 0;
						switch(msg.severity){
							case 0: // Emergency
							case 1:	// Alert
							case 2: // Critical
								severity = 3;
								break;
							case 3: // Error
							case 4: // Warning
							case 5: // Notice
								severity = 2;
								break;
							default: // Normal operation
								severity = 1;
							}
						commsProxy.notifyTitaniumGPSStatus(severity);
						break;
					}
				}
			}
		}
	};
	

}
